<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="DS1302 我们前边也有提起过，是三根线，分别是 CE、I/O 和 SCLK，其中 CE 是使能线，SCLK 是时钟线，I/O 是数据线。前边我们介绍过了 SPI 通信，同学们发现没发现，这个 DS1302的通信线定义和 SPI 怎么这么像呢？ 事实上，DS1302 的通信是 SPI 的变异种类，它用了 SPI 的通信时序，但是通信的时候没有完全按照 SPI 的规则来，下面我们一点点解剖 DS1">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="15.6、DS1302通信时序介绍">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu102/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="DS1302 我们前边也有提起过，是三根线，分别是 CE、I/O 和 SCLK，其中 CE 是使能线，SCLK 是时钟线，I/O 是数据线。前边我们介绍过了 SPI 通信，同学们发现没发现，这个 DS1302的通信线定义和 SPI 怎么这么像呢？ 事实上，DS1302 的通信是 SPI 的变异种类，它用了 SPI 的通信时序，但是通信的时候没有完全按照 SPI 的规则来，下面我们一点点解剖 DS1">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322000P6107.png">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322000Z0B9.png">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322001005C6.png">
<meta property="og:updated_time" content="2019-01-31T13:12:45.596Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="15.6、DS1302通信时序介绍">
<meta name="twitter:description" content="DS1302 我们前边也有提起过，是三根线，分别是 CE、I/O 和 SCLK，其中 CE 是使能线，SCLK 是时钟线，I/O 是数据线。前边我们介绍过了 SPI 通信，同学们发现没发现，这个 DS1302的通信线定义和 SPI 怎么这么像呢？ 事实上，DS1302 的通信是 SPI 的变异种类，它用了 SPI 的通信时序，但是通信的时候没有完全按照 SPI 的规则来，下面我们一点点解剖 DS1">
<meta name="twitter:image" content="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322000P6107.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu102/">





  <title>15.6、DS1302通信时序介绍 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu102/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">15.6、DS1302通信时序介绍</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:41:02+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>DS1302 我们前边也有提起过，是三根线，分别是 CE、I/O 和 SCLK，其中 CE 是使能线，SCLK 是时钟线，I/O 是数据线。前边我们介绍过了 SPI 通信，同学们发现没发现，这个 DS1302的通信线定义和 SPI 怎么这么像呢？</p>
<p>事实上，DS1302 的通信是 SPI 的变异种类，它用了 SPI 的通信时序，但是通信的时候没有完全按照 SPI 的规则来，下面我们一点点解剖 DS1302 的变异 SPI 通信方式。先看一下单字节写入操作，如图 15-11 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322000P6107.png" alt></p>
<center><strong>图 15-11  DS1302 单字节写操作</strong></center>

<p>然后我们再对比一下 CPOL=0/CPHA=0 情况下的 SPI 的操作时序，如图 15-12 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322000Z0B9.png" alt></p>
<center><strong>图 15-12  CPOL=0/CPHA=0 通信时序</strong></center>


<p>图 15-11 和图 15-12 的通信时序，其中 CE 和 SSEL 的使能控制是反的，对于通信写数据，都是在 SCK 的上升沿，从机进行采样，下降沿的时候，主机发送数据。DS1302 的时序里，单片机要预先写一个字节指令，指明要写入的寄存器的地址以及后续的操作是写操作，然后再写入一个字节的数据。</p>
<p>对于单字节读操作，我就不做对比了，把 DS1302 的时序图贴出来，大家自己看一下即可，如图 15-13 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140322/1-140322001005C6.png" alt></p>
<center><strong>图 15-13  DS1302 单字节读操作</strong></center>

<p>读操作有两处需要特别注意的地方。第一，DS1302 的时序图上的箭头都是针对 DS1302来说的，因此读操作的时候，先写第一个字节指令，上升沿的时候 DS1302 来锁存数据，下降沿我们用单片机发送数据。到了第二个字数据，由于我们这个时序过程相当于CPOL=0/CPHA=0，前沿发送数据，后沿读取数据，第二个字节是 DS1302 下降沿输出数据，我们的单片机上升沿来读取，因此箭头从 DS1302 角度来说，出现在了下降沿。</p>
<p>第二个需要注意的地方就是，我们的单片机没有标准的 SPI 接口，和 I2C 一样需要用 IO口来模拟通信过程。在读 DS1302 的时候，理论上 SPI 是上升沿读取，但是程序是用 IO 口模拟的，所以数据的读取和时钟沿的变化不可能同时了，必然就有一个先后顺序。通过实验发现，如果先读取 IO 线上的数据，再拉高 SCLK 产生上升沿，那么读到的数据一定是正确的，而颠倒顺序后数据就有可能出错。这个问题产生的原因还是在于 DS1302 的通信协议与标准SPI 协议存在的差异造成的，如果是标准 SPI 的数据线，数据会一直保持到下一个周期的下降沿才会变化，所以读取数据和上升沿的先后顺序就无所谓了；但 DS1302 的 IO 线会在时钟上升沿后被 DS1302 释放，也就是撤销强推挽输出变为弱下拉状态，而此时在 51 单片机引脚内部上拉的作用下，IO 线上的实际电平会慢慢上升，从而导致在上升沿产生后再读取 IO 数据的话就可能会出错。因此这里的程序我们按照先读取 IO 数据，再拉高 SCLK 产生上升沿的顺序。</p>
<p>下面我们就写一个程序，先将 2013 年 10 月 8 号星期二 12 点 30 分 00 秒这个时间写到DS1302 内部，让 DS1302 正常运行，然后再不停的读取 DS1302 的当前时间，并显示在我们的液晶屏上。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">（此处省略，可参考之前章节的代码）</span><br><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit DS1302_CE = P1^7;</span><br><span class="line">sbit DS1302_CK = P3^5;</span><br><span class="line">sbit DS1302_IO = P3^4;</span><br><span class="line">bit flag200ms = 0; //200ms 定时标志</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">void InitDS1302();</span><br><span class="line">unsigned char DS1302SingleRead(unsigned char reg);</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">    unsigned char psec=0xAA; //秒备份，初值 AA 确保首次读取时间后会刷新显示</span><br><span class="line">    unsigned char time[8]; //当前时间数组</span><br><span class="line">    unsigned char str[12]; //字符串转换缓冲区</span><br><span class="line">    EA = 1; //开总中断</span><br><span class="line">    ConfigTimer0(1); //T0 定时 1ms</span><br><span class="line">    InitDS1302(); //初始化实时时钟</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">   </span><br><span class="line">    while (1)&#123;</span><br><span class="line">        if (flag200ms)&#123; //每 200ms 读取一次时间</span><br><span class="line">            flag200ms = 0;</span><br><span class="line">            for (i=0; i&lt;7; i++)&#123; //读取 DS1302 当前时间</span><br><span class="line">                time[i] = DS1302SingleRead(i);</span><br><span class="line">            &#125;</span><br><span class="line">            if (psec != time[0])&#123; //检测到时间有变化时刷新显示</span><br><span class="line">                str[0] = &apos;2&apos;; //添加年份的高 2 位：20</span><br><span class="line">                str[1] = &apos;0&apos;;</span><br><span class="line">                str[2] = (time[6] &gt;&gt; 4) + &apos;0&apos;; //“年”高位数字转换为 ASCII 码</span><br><span class="line">                str[3] = (time[6]&amp;0x0F) + &apos;0&apos;; //“年”低位数字转换为 ASCII 码</span><br><span class="line">                str[4] = &apos;-&apos;; //添加日期分隔符</span><br><span class="line">                str[5] = (time[4] &gt;&gt; 4) + &apos;0&apos;; //“月”</span><br><span class="line">                str[6] = (time[4]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[7] = &apos;-&apos;;</span><br><span class="line">                str[8] = (time[3] &gt;&gt; 4) + &apos;0&apos;; //“日”</span><br><span class="line">                str[9] = (time[3]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[10] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(0, 0, str); //显示到液晶的第一行</span><br><span class="line">                str[0] = (time[5]&amp;0x0F) + &apos;0&apos;; //“星期”</span><br><span class="line">                str[1] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(11, 0, &quot;week&quot;);</span><br><span class="line">                LcdShowStr(15, 0, str); //显示到液晶的第一行</span><br><span class="line">                str[0] = (time[2] &gt;&gt; 4) + &apos;0&apos;; //“时”</span><br><span class="line">                str[1] = (time[2]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[2] = &apos;:&apos;; //添加时间分隔符</span><br><span class="line">                str[3] = (time[1] &gt;&gt; 4) + &apos;0&apos;; //“分”</span><br><span class="line">                str[4] = (time[1]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[5] = &apos;:&apos;;</span><br><span class="line">                str[6] = (time[0] &gt;&gt; 4) + &apos;0&apos;; //“秒”</span><br><span class="line">                str[7] = (time[0]&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">                str[8] = &apos;\0&apos;;</span><br><span class="line">                LcdShowStr(4, 1, str); //显示到液晶的第二行</span><br><span class="line">                psec = time[0]; //用当前值更新上次秒数</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 发送一个字节到 DS1302 通信总线上 */</span><br><span class="line">void DS1302ByteWrite(unsigned char dat)&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位移出</span><br><span class="line">        if ((mask&amp;dat) != 0)&#123; //首先输出该位数据</span><br><span class="line">            DS1302_IO = 1;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            DS1302_IO = 0;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_IO = 1; //最后确保释放 IO 引脚</span><br><span class="line">&#125;</span><br><span class="line">/* 由 DS1302 通信总线上读取一个字节 */</span><br><span class="line">unsigned char DS1302ByteRead()&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    unsigned char dat = 0;</span><br><span class="line">   </span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位读取</span><br><span class="line">        if (DS1302_IO != 0)&#123; //首先读取此时的 IO 引脚，并设置 dat 中的对应位</span><br><span class="line">            dat |= mask;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    return dat; //最后返回读到的字节数据</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次写操作向某一寄存器写入一个字节，reg-寄存器地址，dat-待写入字节 */</span><br><span class="line">void DS1302SingleWrite(unsigned char reg, unsigned char dat)&#123;</span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x80); //发送写寄存器指令</span><br><span class="line">    DS1302ByteWrite(dat); //写入字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次读操作从某一寄存器读取一个字节，reg-寄存器地址，返回值-读到的字节 */</span><br><span class="line">unsigned char DS1302SingleRead(unsigned char reg)&#123;</span><br><span class="line">    unsigned char dat;</span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x81); //发送读寄存器指令</span><br><span class="line">    dat = DS1302ByteRead()//读取字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">    return dat;</span><br><span class="line">&#125;</span><br><span class="line">/* DS1302 初始化，如发生掉电则重新设置初始时间 */</span><br><span class="line">void InitDS1302()&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">    unsigned char code InitTime[] = &#123; //2013 年 10 月 8 日 星期二 12:30:00</span><br><span class="line">        0x00,0x30,0x12, 0x08, 0x10, 0x02, 0x13</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 0; //初始化 DS1302 通信引脚</span><br><span class="line">    DS1302_CK = 0;</span><br><span class="line">    i = DS1302SingleRead(0); //读取秒寄存器</span><br><span class="line">   </span><br><span class="line">    if ((i &amp; 0x80) != 0)&#123; //由秒寄存器最高位 CH 的值判断 DS1302 是否已停止</span><br><span class="line">        DS1302SingleWrite(7, 0x00); //撤销写保护以允许写入数据</span><br><span class="line">        for (i=0; i&lt;7; i++)&#123; //设置 DS1302 为默认的初始时间</span><br><span class="line">            DS1302SingleWrite(i, InitTime[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 12; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，执行 200ms 定时 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    static unsigned char tmr200ms = 0;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    tmr200ms++;</span><br><span class="line">    if (tmr200ms &gt;= 200)&#123; //定时 200ms</span><br><span class="line">        tmr200ms = 0;</span><br><span class="line">        flag200ms = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>前边学习了 I2C 和 EEPROM 的底层读写时序，那么 DS1302 的底层读写时序程序的实现方法是与之类似的，这里就不过多解释了，大家自己认真揣摩一下。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu101/" rel="next" title="15.5、DS1302寄存器介绍">
                <i class="fa fa-chevron-left"></i> 15.5、DS1302寄存器介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu103/" rel="prev" title="15.7、S1302的BURST模式">
                15.7、S1302的BURST模式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
